using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.SQLite;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using YahooStocks;
using System.Configuration ;

namespace Harness
{
    public partial class Form1 : Form
    {
         delegate void SetValueDelegate(Object obj, Object val, Object[] index);

        public void SetControlProperty(Control ctrl, String propName, Object val)
        {
            // Usage: SetControlProperty(label1, "Text", myString);
            PropertyInfo propInfo = ctrl.GetType().GetProperty(propName);
            Delegate dgtSetValue = new SetValueDelegate(propInfo.SetValue);
            ctrl.Invoke(dgtSetValue, new Object[3] { ctrl, val, /*index*/ null });
        }

      

        string[] dow = { "MMM", "AA", "MO", "AXP", "AIG", "T ", "BA", "CAT", "C", "KO", "DD", "XOM", "GE", "GM", "HPQ", "HD", "HON", "INTC", "IBM", "JNJ", "JPM", "MCD", "MRK", "MSFT", "PFE", "PG", "UTX", "VZ", "WMT", "DIS" };

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            YahooStocks.Downloader dl = new Downloader();

            foreach (String sym in dow)
            {
                this.txtSymbol.Text = sym;
                DataTable dt = dl.UpdateSymbol(sym, this.dtpStartDate.Value, this.dtpEndDate.Value);

                dl.InsertOrUpdateIssue(dt, txtSymbol.Text);
                dataGridView1.DataSource = dt;
                SetControlProperty(label4, "Text", "   GOT : " + sym +"   ");
                label4.Refresh();
                
                 
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            YahooStocks.Downloader dl = new Downloader();
           string sym= this.txtSymbol.Text;
            DataTable dt = dl.UpdateSymbol(sym, this.dtpStartDate.Value, this.dtpEndDate.Value);
             
            dl.InsertOrUpdateIssue(dt, sym);
            dataGridView1.DataSource = dt;
            SetControlProperty(label4, "Text", "GOT : " + sym + "   ");


        }

        private void btnChart_Click(object sender, EventArgs e)
        {
            FormChart chrt = new FormChart();

            try
            {
                chrt.DoChart(txtSymbol.Text, dtpStartDate.Value, dtpEndDate.Value);


                chrt.ShowDialog();
            }
            catch(Exception ex)
            {
                label4.Text = ex.Message;
            }
        }

        private void txtSymbol_KeyUp(object sender, KeyEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

        private void button1_MouseHover(object sender, EventArgs e)
        {
           
        }

        private void txtSymbol_TextChanged(object sender, EventArgs e)
        {

        }
    }
}